$def with ()

$var title: $_(u'SIP Programs')
$var page: programs
$code:
    snames = gv.snames
    nprogs = len(gv.pd)
	
    def display_name():
	    if prog['name']:
	        return prog['name']
	    else:
	        return _(u'Program') + " " +  str(pid+1)

    def two_digits(n):
        return '%02d' % int(n)
    
    def pdays(days, dayInterval):
        output = ""
        if prog['type'] == 'interval':
            days = days&0x7f
            daysRemaining = ((days + dayInterval)-(gv.dse % dayInterval)) % dayInterval
            output += _(u"Every ") + str(dayInterval) + " " + _(u"days")
            if daysRemaining == 0:
                output += ", " + _(u'starting today') + "."
            elif daysRemaining == 1:
                output += ", " + _(u'starting tomorrow') + "."
            else:
                output += ", " + _(u'starting in') + " " + str(daysRemaining) + " " + _(u'days') + "."
        else: 
            if days == 127:
                output += _(u"Every Weekday")
            else:
                for d in range(0,7):
                    if days & (1<<d):
                        output += " " + [_(u"Mon"),_(u"Tue"),_(u"Wed"),_(u"Thur"),_(u"Fri"),_(u"Sat"),_(u"Sun")][d]
        if prog['type'] == 'evendays':
            output += _(u' (Even days only)')
        if prog['type'] == 'odddays':
            output += _(u' (Odd days only)')
        return output

    def formatTime(t):
        if gv.sd['tf']:
            return t
        else:
            hour = int(t[0:2])
            newhour = hour
            if hour == 0:
                newhour = 12
            if hour > 12:
                newhour = hour-12
            return str(newhour)  + t[2:] + (" am" if hour<12 else " pm")

<script>
    // Initialize behaviors
    jQuery(document).ready(function(){

        jQuery("button#nDelAll, button.cDelete").click(function(){
            try{
                var pid = jQuery(this).attr("data")
                }
            catch(err) {
                var pid = 0
            }
            jQuery("form#df input[name='pid']").val(pid);
            jQuery("form#df").submit();
        });

        jQuery("button#nAdd, button.cModify").click(function(){
            try{
                var pid = jQuery(this).attr("data")
                }
            catch(err) {
                var pid = 0
            }
            jQuery("form#mf input[name='pid']").val(pid);
            jQuery("form#mf").submit();
        });

        jQuery("button.cRunNow").click(function(){
            try{
                var pid = jQuery(this).attr("data")
                }
            catch(err) {
                var pid = 0
            }
            jQuery("form#rn input[name='pid']").val(pid);
            jQuery("form#rn").submit();
        });

            jQuery("button.cDisable").click(function(){
                var pid = jQuery(this).attr("data");
                var enable = jQuery(this).hasClass("off");
                jQuery("form#ep input[name='pid']").val(pid);
                jQuery("form#ep input[name='enable']").val(enable ? 1 : 0);
                jQuery("form#ep").submit();
            });        
        
    });

</script>
<form name="df" id="df" action="/dp" method="get">
        <input type="hidden" name="pid">
</form>
<form name="rn" id="rn" action="/rp" method="get">
    <input type="hidden" name="pid">
</form>
<form name="mf" id="mf" action="/mp" method="get">
    <input type="hidden" name="pid">
</form>
     <form name="ep" id="ep" action="/ep" method="get">
     <input type="hidden" name="pid">
     <input type="hidden" name="enable">
</form>
<div class="controls">
    <button id="nAdd" class="add" data="-1">$_(u'Add a New Program')</button>
    <button id="nDelAll" class="deleteAll danger" data="-1">$_(u'Delete All')</button>
</div>

<div id="programs">
    $for pid in range(nprogs):
        $ prog = gv.pd[pid]
        <div id="p${pid}" class="controlblock program ${'disabled' if prog['enabled'] == 0 else ''}">
         <p>
             <button class="cDisable toggle ${'off' if prog['enabled'] == 0 else 'on'}" data="${pid}">
                 <span class='toggleleft'>$_(u'On')</span>
                 <span class='togglesep'>&nbsp;</span>
                 <span class='toggleright'>$_(u'Off')</span>
             </button>
             <span class="val">${display_name()}</span>: <span>${pdays(prog['day_mask'], prog['interval_base_day'])}</span></b>
         </p>
            $ start = prog['start_min']
            $ end = prog['stop_min']
            $ interval = prog['cycle_min']
            $ duration = 0
            <p class="stationList">$_(u'Run'):
			$ sid = -1
            $for bid in range(gv.sd['nbrd']):
                $if len(prog) >= 8 + bid and isinstance(prog['station_mask'][bid], int):
                    $ bits = prog['station_mask'][bid]
                $else:
                    $ bits = 0
                $for s in range(8):
                    $ sid += 1
                    $if (gv.sd['show'][bid]>>s)&1 and bits&(1 << s):
                        <span class="stationName val">${snames[sid]}</span>
                        $if gv.sd['seq'] :
                            $if gv.sd['idd'] :
                                $ duration += prog['duration_sec'][sid]
                            $else:
                                $ duration += prog['duration_sec'][0] # added + ###
                        $else:
                            $if gv.sd['idd'] :
                                $ duration = max(duration, prog['duration_sec'][sid])
                            $else:
                                $ duration = prog['duration_sec'][0]
            $ lap_end = start + duration / 60
            $ recurring = prog['cycle_min'] > 0 #  int(lap_end) < int(end)
							
            </p>
            <p>$_(u'Starting'): <span class="val">${formatTime(two_digits(start/60) + ":" + two_digits(start%60))}</span>
                <span ${"" if (gv.sd['idd'] > 0) else "style=display:none"}>$_(u'with individual station durations')</span></p>
                <p>$_(u'for')</b> <span class="val">${two_digits(duration/3600)}</span> hrs <span class="val">${two_digits(((duration/60)%60))}</span> mins <span class="val">${two_digits(duration%60)}</span> secs 
            $if not recurring:    
                $_(u'until') <span class="val">${formatTime(two_digits(lap_end/60) + ":" + two_digits(lap_end%60))}</span></p>
            $else:
                </p>
            $if recurring:
                <p>$_(u'Recurring every')</b> <span class="val">${two_digits(interval/60)}</span> hrs <span class="val">${two_digits(interval%60)}</span> mins
				$_(u'until') <span class="val">${formatTime(two_digits(end/60) + ":" + two_digits(end%60))}</span></p>

            <div class="controls">
                <button class="cRunNow" data="${pid}">$_(u'Run Now')</button>
                <button class="cModify" data="${pid}">$_(u'Edit')</button>
                <button class="cDelete danger" data="${pid}">$_(u'Delete')</button>
            </div>
        </div>
</div>